package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetMealDishMapper;
import com.itheima.reggie.mapper.SetMealMapper;
import com.itheima.reggie.service.SetMealService;
import com.itheima.reggie.vo.MyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private SetMealDishMapper setMealDishMapper;

    @Override
    public Page<SetMeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        Page<SetMeal> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SetMeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), SetMeal::getName, name);
        Page<SetMeal> page1 = setMealMapper.selectPage(page, wrapper);

        //根据category_id获取套餐名称,并给套餐分类名称赋值
        List<SetMeal> records = page1.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            for (SetMeal record : records) {
                Category category = categoryMapper.selectById(record.getCategoryId());
                record.setCategoryName(category.getName());
            }
        }
        return page1;
    }

    @Override
    public void add(SetMeal setMeal) {
        //将接受的对象数据存进数据库
        setMealMapper.insert(setMeal);

        //用setmealdish存储前端传的集合
        List<SetMealDish> setmealDishes = setMeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishes)) {
            for (SetMealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setMeal.getId());
                setMealDishMapper.insert(setmealDish);
            }
        }

    }

    @Override
    public void stopAndStart(Integer status, MyUtil myUtil) {
        List<SetMeal> setMeals = setMealMapper.selectBatchIds(myUtil.getIds());
        if (CollectionUtil.isNotEmpty(setMeals)) {
            for (SetMeal setMeal : setMeals) {
                setMeal.setStatus(status);
                LambdaQueryWrapper<SetMeal> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(SetMeal::getId, setMeal.getId());
                setMealMapper.update(setMeal, wrapper);
            }
        }
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        int count = 0;
        if (CollectionUtil.isNotEmpty(ids)) {
            List<SetMeal> setMeals = setMealMapper.selectBatchIds(ids);
            if (CollectionUtil.isNotEmpty(setMeals)) {
                for (SetMeal setMeal : setMeals) {
                    if (setMeal.getStatus() == 0) {
                        LambdaQueryWrapper<SetMeal> wrapper = new LambdaQueryWrapper<>();
                        wrapper.eq(SetMeal::getId, setMeal.getId());
                        setMealMapper.delete(wrapper);
                        //根据setmeal_id删除setmealdish
                        LambdaQueryWrapper<SetMealDish> wrapper1 = new LambdaQueryWrapper<>();
                        wrapper1.eq(SetMealDish::getSetmealId,setMeal.getId());
                        setMealDishMapper.delete(wrapper1);
                    } else {
                        count++;
                    }
                }
            }
        }
        throw new CustomException(ids.size()-count+"条数据删除成功，当前选中有："+ count+"条启售状态的菜品，启售状态菜品不可删除");
    }

    //todo
    @Override
    public SetMeal findById(Long id) {
        SetMeal setMeal = setMealMapper.selectById(id);
        //根据套餐的id查询套餐下的菜品
        LambdaQueryWrapper<SetMealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetMealDish::getDishId,setMeal.getId());
        List<SetMealDish> setMealDishes = setMealDishMapper.selectList(wrapper);
        setMeal.setSetmealDishes(setMealDishes);
        return setMeal;
    }

    @Override
    public void update(SetMeal setMeal) {
        //更新参数都是对象
        setMealMapper.updateById(setMeal);
        //删除当前套餐下的菜品
        LambdaQueryWrapper<SetMealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetMealDish::getSetmealId,setMeal.getId());
        setMealDishMapper.delete(wrapper);

        //添加修改后的菜品进套餐菜品表
        List<SetMealDish> setmealDishes = setMeal.getSetmealDishes();
        if(CollectionUtil.isNotEmpty(setmealDishes)){
            for (SetMealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setMeal.getId());
                setMealDishMapper.insert(setmealDish);
            }
        }
    }

    //前端查询
    @Override
    public List<SetMeal> findList(Long id, Integer status) {
        LambdaQueryWrapper<SetMeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetMeal::getCategoryId,id).eq(SetMeal::getStatus,status);
        List<SetMeal> setMeals = setMealMapper.selectList(wrapper);


        return setMeals;
    }

    @Override
    public List<SetMealDish> findDishesById(Long id) {
        LambdaQueryWrapper<SetMealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetMealDish::getSetmealId,id);
        List<SetMealDish> dishes = setMealDishMapper.selectList(wrapper);
        return dishes;
    }

}
